class ListNode {
    int val;
    //套定义ListNode类型
    ListNode next;
    ListNode(int x) { val = x; }
}

class MergeTwoSortedLinkedLists {
    public static void main(String[] args) {
        // 创建第一个有序链表 1 -> 2 -> 4
        ListNode list1 = new ListNode(1);
        list1.next = new ListNode(2);
        list1.next.next = new ListNode(4);

        // 创建第二个有序链表 1 -> 3 -> 4
        ListNode list2 = new ListNode(1);
        list2.next = new ListNode(3);
        list2.next.next = new ListNode(4);

        ListNode mergedList = Solution.mergeTwoLists(list1, list2);

        // 打印合并后的链表
        // 打印合并后的链表
        while (mergedList != null) {
            System.out.print(mergedList.val + " ");
            mergedList = mergedList.next;
        }
    }

     static class Solution {
        public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
            ListNode back = new ListNode(-1);
            ListNode point = back;

            while (list1 != null && list2 != null) {
                if (list1.val >= list2.val) {
                    point.next = list2;
                    list2 = list2.next;
                } else {
                    point.next = list1;
                    list1 = list1.next;
                }
                point = point.next;
            }

            if (list1 != null) {
                point.next = list1;
            }
            if (list2 != null) {
                point.next = list2;
            }

            return back.next;
        }
    }
}